from django.db import models

# Create your models here.

class Category(models.Model):
    name = models.CharField('类别名称', max_length=100, unique=True)
    description = models.TextField('描述', blank=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '类别'
        verbose_name_plural = '类别管理'

class Food(models.Model):
    name = models.CharField('名称', max_length=200)
    category = models.ForeignKey(Category, verbose_name='所属类别', on_delete=models.CASCADE, related_name='foods')
    quantity = models.DecimalField('数量', max_digits=10, decimal_places=2)
    unit = models.CharField('单位', max_length=50)
    expiry_date = models.DateField('保质期')
    storage_location = models.CharField('存储位置', max_length=100)
    created_at = models.DateTimeField('创建时间', auto_now_add=True)

    def __str__(self):
        return f"{self.name} ({self.quantity} {self.unit})"

    class Meta:
        verbose_name = '食品'
        verbose_name_plural = '食品管理'

class Purchase(models.Model):
    food = models.ForeignKey(Food, verbose_name='食品', on_delete=models.CASCADE, related_name='purchases')
    quantity = models.DecimalField('数量', max_digits=10, decimal_places=2)
    price = models.DecimalField('价格', max_digits=10, decimal_places=2)
    purchase_date = models.DateField('采购日期')

    def __str__(self):
        return f"{self.food.name} - {self.purchase_date}"

    class Meta:
        verbose_name = '采购记录'
        verbose_name_plural = '采购管理'
